﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using élénkPróbája.extentions;
using élénkPróbája.Test.dto;
using élénkPróbája.Test.extention;

namespace élénkPróbája.Test
{
    [TestClass]
    public class EmailValidatorTester
    {
        TestValidator validator;

        public EmailValidatorTester()
        {
            CultureScope.SetDefaultCulture();

            validator = new TestValidator {
                v => v.RuleFor(x => x.Owner.Email).EmailAddress()
            };
        }
        [TestMethod]
        public void When_the_text_is_empty_then_the_validator_should_fail()
        {
            string email = String.Empty;
            var result = validator.Validate(new Company() { Owner = new Person() { Email = email } });
            result.IsValid.ShouldBeFalse();
        }
        [TestMethod]
        public void When_the_text_is_not_a_valid_email_address_then_the_validator_should_fail()
        {
            string email = "testperson";
            var result = validator.Validate(new Company() { Owner = new Person() { Email = email } });
            result.IsValid.ShouldBeFalse();
        }
        [TestMethod]
        public void When_validation_fails_the_default_error_should_be_set()
        {
            string email = "testperson";
            var result = validator.Validate(new Company() { Owner = new Person() { Email = email } });
            result.Errors.Single().ErrorMessage.ShouldEqual("'' is not a valid email address.");
        }
        [TestMethod]
        public void This_should_not_hang()
        {
            string email = "thisisaverylongstring.com";
            var result = validator.Validate(new Company() { Owner = new Person() { Email = email } });
            result.IsValid.ShouldBeFalse();
        }
        [TestMethod]
        public void double_period_with_uk_Domain()
        {
            string email = "first.last@firstmerit..co.uk";
            var result = validator.Validate(new Company() { Owner = new Person() { Email = email } });
            result.IsValid.ShouldBeFalse();
        }
        [TestMethod]
        public void Valid_email_addresses()
        {
            var emails = new List<string>() {"testperson@gmail.com", "TestPerson@gmail.com" , "testperson+label@gmail.com" , "\"Abc\\@def\"@example.com" , "\"Fred Bloggs\"@example.com" , "\"Joe\\Blow\"@example.com" , "\"Abc@def\"@example.com" , "customer/department=shipping@example.com" , "$A12345@example.com" , "!def!xyz%abc@example.com" , "__somename@example.com" , "first.last@test.co.uk" };
            foreach (var email in emails)
            {
                var result = validator.Validate(new Company() { Owner = new Person() { Email = email } });
                result.IsValid.ShouldBeTrue($"The email address {email} should be valid");
            } 
        }
    }
}
